package chapter05

import scala.annotation.tailrec

object Test12_MyWhile {

  def main(args: Array[String]): Unit = {
     // 自定义while 关键字

    var n = 0;
    while(n>=1){
      println(n)
      n -= 1
    }

    // 用闭包实现一个函数，将代码块作为参数传入，递归调用

    def myWhile(condition: =>Boolean ): (=>Unit)=>Unit ={
      // 内层还是需要递归调用，参数就是循环体
      def doLoop(op: =>Unit):Unit = {
        if(condition){
          op
          myWhile(condition)
        }
      }
      doLoop _
    }

    def myWhile2(condition: =>Boolean ): (=>Unit)=>Unit =
      op=> {
        if(condition){
          op
          myWhile2(condition)
        }
      }

    def myWhile3(condition: =>Boolean )(op: =>Unit): Unit =
      {
        if(condition){
          op
          myWhile3(condition)(op)
        }
      }


    myWhile(n<=0){
      println(n)
      n-=1
    }

  }

}
